package com.lnkj.spcj.ui.nfc

import android.app.PendingIntent
import android.content.Intent
import android.content.IntentFilter
import android.content.IntentFilter.MalformedMimeTypeException
import android.nfc.NdefMessage
import android.nfc.NfcAdapter
import android.nfc.tech.IsoDep
import android.nfc.tech.NfcF
import android.nfc.tech.NfcV
import android.widget.Toast
import com.alibaba.fastjson.JSON
import com.lnkj.spcj.R
import com.lnkj.spcj.base.BaseActivity2
import com.lnkj.spcj.ui.camera.CameraActivity
import com.lnkj.spcj.ui.scan.ScanResultBean
import com.lnkj.spcj.util.utilcode.LogUtils
import org.jetbrains.anko.startActivity
import java.io.UnsupportedEncodingException
import java.nio.charset.Charset


class NfcActivity : BaseActivity2() {
    override val layoutRes: Int
        get() = R.layout.activity_nfc
    lateinit var nfcAdapter: NfcAdapter // NFC适配器
    lateinit var pendingIntent: PendingIntent // NFC前台调度系统
    lateinit var tenchlists: Array<Array<String>>
    lateinit var filters: Array<IntentFilter>


    override fun initAll() {
        tenchlists = arrayOf(arrayOf(IsoDep::class.java.name), arrayOf(NfcV::class.java.name), arrayOf(NfcF::class.java.name))
        try {
            filters = arrayOf(IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED, "*/*"))
        } catch (e1: MalformedMimeTypeException) {
            e1.printStackTrace()
        }
        // 初始化PendingIntent，当有NFC设备连接上的时候，就交给当前Activity处理
        pendingIntent = PendingIntent.getActivity(this, 0, Intent(this, javaClass).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0)
        // 获取默认的NFC控制器，并进行判断
        nfcAdapter = NfcAdapter.getDefaultAdapter(this)
        if (!nfcAdapter.isEnabled) {
            Toast.makeText(applicationContext, "请在系统设置中先启用NFC功能！", Toast.LENGTH_SHORT).show()
            LogUtils.dTag("h_bl", "请在系统设置中先启用NFC功能！")
            return
        }
        val intent = this.intent // 捕获NFC Intent
        praseIntent(intent)
    }

    override fun setListener() {

    }

    override fun processLogic() {

    }


    private fun readFromTag(intent: Intent): Boolean {
        val rawArray = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES)
        val mNdefMsg = rawArray[0] as NdefMessage
        val mNdefRecord = mNdefMsg.records[0]
        try {
            if (mNdefRecord != null) {
                var result = String(mNdefRecord.payload, Charset.forName("UTF-8"))
                var dataBean = JSON.parseObject(result, ScanResultBean.DataBean::class.java)
                var bean = ScanResultBean()
                bean.status = 1
                bean.info = "nfc读取"
                bean.data = dataBean
                startActivity<CameraActivity>("bean" to bean)
                finish()
                return true
            }
        } catch (e: UnsupportedEncodingException) {
            e.printStackTrace()
        }
        return false
    }

    fun praseIntent(intent: Intent) {
        val nfcAction = intent.action
        if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(nfcAction)) {
            readFromTag(intent)
        }
    }
}